package com.aurora.oa.mycenter.view;

import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;

import com.aurora.oa.R;
import com.aurora.oa.activity.BaseActivity;
import com.aurora.oa.adapter.CommonRecyclerViewAdapter;
import com.aurora.oa.adapter.CommonRecyclerViewHolder;
import com.aurora.oa.customview.ItemDivider;
import com.aurora.oa.listener.OnGetMyTeamListListener;
import com.aurora.oa.model.RequestHttpModel;
import com.aurora.oa.model.RequestHttpModelImpl;
import com.aurora.oa.mycenter.bean.TeamBean;
import com.aurora.oa.utils.ToastUtil;

import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;

/**
 * Created by Administrator on 2017/5/3 0003.
 */
public class MyTeamListActivity extends BaseActivity {
    CommonRecyclerViewAdapter<TeamBean> adapter;
    List<TeamBean> teamBeanList = new ArrayList<>();
    @Bind(R.id.recyclerView)
    RecyclerView recyclerView;
    LinearLayoutManager linearLayoutManager;
    RequestHttpModel model = new RequestHttpModelImpl();
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        initTitlebar(true, "我的团队", true);
        setMyContentView(R.layout.activity_myteam_list);
        ButterKnife.bind(this);
        initView();
        initData();
    }

    private void initData() {
        model.getMyTeamList(new OnGetMyTeamListListener() {
            @Override
            public void onGetMyTeamListSuccess(List<TeamBean> teamBeanList) {
                refreshList(teamBeanList);
            }

            @Override
            public void onGetMyTeamListFail(String message) {
                ToastUtil.show(MyTeamListActivity.this,message);;
            }
        });
    }

    private void refreshList(List<TeamBean> teamBeanList) {
        teamBeanList.clear();
        teamBeanList.addAll(teamBeanList);
        adapter.notifyDataSetChanged();
    }

    private void initView() {
        linearLayoutManager = new LinearLayoutManager(this);
        adapter = new CommonRecyclerViewAdapter<TeamBean>(this,teamBeanList) {
            @Override
            public void convert(CommonRecyclerViewHolder h, TeamBean entity, int position) {
                h.setText(R.id.tv_teamname,entity.name);
            }

            @Override
            public int getLayoutViewId(int viewType) {
                return R.layout.item_my_team;
            }
        };
        adapter.setOnRecyclerViewItemClickListener(new CommonRecyclerViewAdapter.OnRecyclerViewItemClickListener() {
            @Override
            public void onItemClick(View v, int position) {
                Intent intent = new Intent(MyTeamListActivity.this,MyMemberListActivity.class);
                intent.putExtra("id",teamBeanList.get(position).id);
                intent.putExtra("type",1);
                startActivity(intent);
            }
        });
        recyclerView.setLayoutManager(linearLayoutManager);
        recyclerView.addItemDecoration(new ItemDivider().setDividerWith(3).setDividerColor(Color.TRANSPARENT));
        recyclerView.setAdapter(adapter);
    }
}
